package com.young.manager.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.young.common.execption.ServiceException;
import com.young.manager.mapper.SysRoleMapper;
import com.young.manager.mapper.SysRoleMenuMapper;
import com.young.manager.mapper.SysUserRoleMapper;
import com.young.manager.model.SysRole;
import com.young.manager.model.SysRoleMenu;
import com.young.manager.model.SysUserRole;
import com.young.manager.service.SysRoleService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RequiredArgsConstructor
@Service
public class SysRoleServiceImpl implements SysRoleService {

    private final SysRoleMapper sysRoleMapper;
    private final SysRoleMenuMapper sysRoleMenuMapper;
    private final SysUserRoleMapper sysUserRoleMapper;

    @Override
    public Page<SysRole> queryList(String name, Integer pageNum, Integer pageSize) {
        QueryWrapper<SysRole> queryWrapper = new QueryWrapper<>();
        if (StringUtils.hasLength(name)) queryWrapper.eq("name", name);
        Page<SysRole> page = new Page<>(pageNum, pageSize);
        return sysRoleMapper.selectPage(page, queryWrapper);
    }

    @Override
    public void insertSysRole(SysRole sysRole) {
        if (!StringUtils.hasLength(sysRole.getName())) throw new ServiceException("参数缺失[name]！");
        QueryWrapper<SysRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name", sysRole.getName());
        if (sysRoleMapper.exists(queryWrapper)) throw new ServiceException("角色名称已存在！");
        if (sysRoleMapper.insert(sysRole) > 0) {
            List<Long> menuIdList = sysRole.getMenuIdList();
            if (null != menuIdList && !menuIdList.isEmpty()) {
                menuIdList.forEach(it -> {
                    SysRoleMenu roleMenu = new SysRoleMenu();
                    roleMenu.setRoleId(sysRole.getId());
                    roleMenu.setMenuId(it);
                    sysRoleMenuMapper.insert(roleMenu);
                });
            }
        }
    }

    @Override
    public void updateSysRole(SysRole sysRole) {
        if (null == sysRole.getId()) throw new ServiceException("参数缺失[id]！");
        if (null == sysRole.getName()) throw new ServiceException("参数缺失[name]！");
        QueryWrapper<SysRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name", sysRole.getName());
        SysRole exist = sysRoleMapper.selectOne(queryWrapper);
        if (null != exist && !exist.getId().equals(sysRole.getId())) throw new ServiceException("角色名称已存在！");
        if (sysRoleMapper.updateById(sysRole) > 0) {
            List<Long> menuIdList = sysRole.getMenuIdList();
            if (null != menuIdList && !menuIdList.isEmpty()) {
                QueryWrapper<SysRoleMenu> roleMenuWrapper = new QueryWrapper<>();
                roleMenuWrapper.eq("role_id", sysRole.getId());
                if (sysRoleMenuMapper.delete(roleMenuWrapper) >= 0) {
                    menuIdList.forEach(it -> {
                        SysRoleMenu roleMenu = new SysRoleMenu();
                        roleMenu.setRoleId(sysRole.getId());
                        roleMenu.setMenuId(it);
                        sysRoleMenuMapper.insert(roleMenu);
                    });
                }
            }
        }
    }

    @Override
    public void deleteSysRole(Long id) {
        Map<String, Object> columnMap = new HashMap<>();
        columnMap.put("role_id", id);
        List<SysUserRole> userRoleList = sysUserRoleMapper.selectByMap(columnMap);
        if (!userRoleList.isEmpty()) throw new ServiceException("角色已被用户关联，请先取消关联！");
        if (sysRoleMapper.deleteById(id) > 0) {
            sysRoleMenuMapper.deleteByMap(columnMap);
        }
    }
}
